package com.stxx.stiot2.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.stxx.framwork.domain.model.Option;
import com.stxx.framwork.domain.model.PageQuery;
import com.stxx.framwork.domain.model.PageResult;
import com.stxx.framwork.utils.StoreUtils;
import com.stxx.stiot2.domain.SysConfig;
import com.stxx.stiot2.service.SysConfigService;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Path;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fuzhenlei
 * @date 2025/01/15 16:41
 */
@Component
public class SysConfigServiceImpl implements SysConfigService {
    @Inject
    StoreUtils storeUtils;

    @Override
    public SysConfig queryById(String id) {
        try {
            return storeUtils.selectById(id, SysConfig.class);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(SysConfig bean) throws SQLException {
        validEntityBeforeSave(bean);
        return storeUtils.insert(bean);
    }

    @Override
    public int update(SysConfig bean) throws SQLException {
        cache.remove(bean.getCode());
        validEntityBeforeSave(bean);
        return storeUtils.update(bean);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysConfig bean){

    }




    @Override
    public int delete(String name) throws SQLException {
        cache.remove(name);
        return storeUtils.delete(SysConfig.class, name);
    }

    @Override
    public int deleteByIds(String[] ids) throws SQLException {
        for(String id : ids){
            cache.remove(id);
        }
        return storeUtils.deleteByIds(SysConfig.class, ids);
    }

    @Override
    public List<SysConfig> list(SysConfig obj) throws SQLException {
        return storeUtils.selectList(SysConfig.class,obj);
    }

    @Override
    public PageResult<SysConfig> pageList(SysConfig obj, PageQuery pageQuery) throws SQLException {
        return storeUtils.selectPageList(SysConfig.class,obj,pageQuery);
    }

    private final static Map<String, JSONObject> cache = new HashMap<String, JSONObject>();
    @Override
    public JSONObject getConfig(String code) {
        if(cache.containsKey(code)){
            return cache.get(code);
        }
        try {
            SysConfig config = storeUtils.sql("select * from "+storeUtils.getTableName(SysConfig.class)+" where code = ?",code).queryRow(SysConfig.class);
            if(config==null){
                return null;
            }
            cache.put(config.getCode(),config.getParams());
            return config.getParams();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public List<Option> options(@Path String code) {
        JSONObject jsonObject = this.getConfig(code);
        String str = jsonObject.getString("value");
        String[] list = str.split("\n");
        List<Option> r = new ArrayList<>();
        for(String str1 : list) {
            String[] strArr = str1.trim().split(":");
            Option option = new Option();
            option.setValue(strArr[0]);
            option.setLabel(strArr[1]);
            r.add(option);
        }
        return r;
    }

    @Override
    public String getStr(String code){
        JSONObject jsonObject = this.getConfig(code);
        return jsonObject.getString("value");
    }
}
